<template>
    <addFileView ref="addFileView" />
    <div class="app-table">
        <div class="app-body">
            <div class="app-search">
                <el-form :inline="true" class="demo-form-inline" v-model="form">
                    <el-form-item label="姓名" style="width:140px">
                        <el-input placeholder="请输入姓名" v-model="form.teacherName"></el-input>
                    </el-form-item>
                    <el-form-item label="类型" style="width:140px">
                        <el-select clearable v-model="form.signType" autocomplete="off" placeholder="请选择">
                            <el-option v-for="item in signTypeList" :key="item.signType" :value="item.signType"
                                :label="item.signTypeName"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="结果" style="width:140px">
                        <el-select clearable v-model="form.signState" autocomplete="off" placeholder="请选择">
                            <el-option :key="0" :value="0" label="未签到"></el-option>
                            <el-option :key="1" :value="1" label="已签到"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="起止时间" style="width:300px">
                        <el-config-provider :locale="locale">
                            <el-date-picker v-model="form.duration" type="daterange" range-separator="To"
                                start-placeholder="开始时间" end-placeholder="结束时间" value-format="YYYY-MM-DD"
                                :size="large" />
                        </el-config-provider>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit" @keyup.enter="searchEnter">搜索</el-button>
                        <el-button type="success" @click="exportExcel">下载数据</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-table size="small" :data="list" border :row-class-name="tableRowClassName">
                <el-table-column prop="id" label="序号" />
                <el-table-column prop="teacherName" label="姓名" />
                <el-table-column prop="gradeId" label="年级" />
                <el-table-column prop="classId" label="班级" />
                <el-table-column prop="signDate" label="签到日期" />
                <el-table-column prop="typeName" label="考勤类型" />
                <el-table-column prop="signState" label="考勤结果" />
                <el-table-column prop="submitter" label="上报人员" />
                <el-table-column prop="submitTime" label="上报时间" />
                <template v-slot:empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>
            <div style="margin-top: 10px">
                <el-config-provider :locale="locale">
                    <el-pagination :current-page="page" :total="total" :page-size="limit"
                        :page-sizes="[5, 10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper"
                        @size-change="changePageSize" @current-change="changeCurrentPage">
                    </el-pagination>
                </el-config-provider>
            </div>
        </div>
        <bottomView></bottomView>
    </div>
</template>
<script>
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";

export default {
    name: "TeacherSignInStatisticsListView",
    data() {
        return {
            type: 0,
            title: "",
            page: 1,
            pageSize: 10,
            total: 0,
            limit: 10,
            list: [],
            signTypeList: [],
            form: {
                teacherName: "",
                duration: "",
                signType: "",
                signState: ""
            },
        };
    },
    components: {
        addFileView,
        IconPicture,
        ElConfigProvider,
        ElMessageBox,
        bottomView,
        ElMessage, Upload
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        let date = new Date();
        let month = ((date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)),
            year = date.getFullYear(), day = ((date.getDate() + 1) >= 10 ? (date.getDate() + 1) : "0" + (date.getDate() + 1));
        let temp = [year + '-' + month + '-' + day, year + '-' + month + '-' + day];
        this.form.duration = temp;
        this.getHeaderTeacherSignList();
        this.getSignTypeList()
    },
    methods: {
        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.getHeaderTeacherSignList();
            }
        },
        calculation(createDate) {
            ElMessage({
                message: '数据计算中，请稍后',
                type: 'success',
            });
            httpService(
                "/pythonApi/attendanceApi/recalculation",
                {
                    'createDate': createDate
                },
                "get"
            ).then((data) => {
                if (data.code == 200) {
                    ElMessage({
                        message: '计算完成',
                        type: 'success',
                    });
                    this.getHeaderTeacherSignList()
                }
            });
        },
        deleteTeacherSignInStatistics(createDate) {
            ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/pythonApi/attendanceApi/deleteData", "createDate=" + createDate, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("删除成功");
                                this.getHeaderTeacherSignList();
                            } else {
                                ElMessage.error("删除失败，请重试！");
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },
        reloadData() {
            this.page = 1;
            this.getHeaderTeacherSignList()
        },
        onSubmit() {
            this.page = 1;
            this.getHeaderTeacherSignList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getHeaderTeacherSignList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getHeaderTeacherSignList();
        },
        getHeaderTeacherSignList() {
            httpService(
                "/pythonApi/attendanceApi/getHeaderSignList",
                {
                    page: this.page,
                    limit: this.limit,
                    duration: this.form.duration,
                    teacherName: this.form.teacherName,
                    signType: this.form.signType ? this.form.signType : "",
                    signState: this.form.signState ? this.form.signState : "",
                },
                "get"
            ).then((data) => {
                this.list = data.data;
                this.total = data.total;
                this.pageCount = data.pages;
            });
        },
        getSignTypeList() {
            httpService(
                "/pythonApi/attendanceApi/getSignTypeList",
                {
                    usedDepartment: 78
                },
                "get"
            ).then((data) => {
                this.signTypeList = data.data;
            });
        },
        exportExcel() {
            let config = { "responseType": "blob" }, temp = this.form.duration;
            let fileName = temp[0].toString() + '到' + temp[1].toString();
            httpService("/pythonApi/attendanceApi/getHeaderTeacherSignData",
                {
                    duration: this.form.duration,
                    teacherName: this.form.teacherName,
                    signType: this.form.signType,
                    signState: this.form.signState
                }, 'get', config).then(
                    (data) => {
                        let link = document.createElement('a'), fileUrl = window.URL.createObjectURL(new Blob([data]));
                        link.style.display = 'none';
                        link.href = fileUrl;
                        link.setAttribute('download', fileName + '班主任考勤表.xlsx');
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                        window.URL.revokeObjectURL(fileUrl)
                    }
                )
        }
    },
};
</script>
<style scoped>
.app-table {
    background: #eff3f5;
    padding: 10px;
    width: calc(100vw - 193px);
}

.app-search {}


.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
    min-height: calc(100vh - 243px);
}
</style>
